import 'package:flutter/cupertino.dart';
import 'package:flutter_app/manager/UserInfoManager.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/reg_exp_utils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';

import 'password_change_state.dart';
/**
 * @Description: 修改密码
 * @Author: 下雨了
 * @CreateDate: 2021-12-30 10:20
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-30 10:20
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class PasswordChangeLogic extends GetxController {
  final PasswordChangeState state = PasswordChangeState();

  late TextEditingController userName, oldPassword, password, password2;

  @override
  void onInit() {
    super.onInit();
    userName = TextEditingController();
    oldPassword = TextEditingController();
    password = TextEditingController();
    password2 = TextEditingController();
  }

  @override
  void onReady() {
    super.onReady();
  }


  /// 修改密码
  /// loginName   账号
  /// oldPassword 当前密码
  /// password    新密码
  Future<void> updateLoginPassword() async {
    if (oldPassword.text.toString() == "") {
      toast.show("请输入原密码");
      return;
    }
    if (!RegExpUtils().isPassword1(password.text.toString())) {
      toast.show("密码长度8~16，包含字母、数字和以下特殊字符（.@\$!%*#_~?&^）");
      return;
    }
    if (password2.text.toString() != password.text.toString()) {
      toast.show("密码不一致");
      return;
    }

    Map<String, dynamic> param = {
      "telephone": UserInfoManager().geTelephone(), // 	手机号
      'oldPwd': oldPassword.text, // 旧登陆密码
      'newPwd': password.text, // 新登陆密码
      'repeatPwd': password.text, // 重复新密码
      "type": "1" // 	改密方式 1.原密码修改 2. 通过手机验证码修改
    };

    ResultData resultData = await Api.updateLoginPassword(param);
    if (resultData.isSuccess) {
      toast.show("密码修改成功");
      UserInfoManager().loginOut();
    } else {
      toast.show(resultData.data["message"]);
    }
  }


  @override
  void onClose() {
    super.onClose();
  }
}
